@extends('owner.base')

@section('content')
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="name" id="name" placeholder="请输入姓名" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="phone" id="phone" placeholder="请输入手机号" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="card_no" id="card_no" placeholder="请输入身份证号" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="car_no" id="card_no" placeholder="请输入车牌号" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" lay-submit lay-filter="search-form">搜 索</button>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" id="tosend" lay-filter="search-form">立即发送</button>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" id="toaddLabel" lay-filter="search-form">创建标签</button>
                </div>

            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>

            {{--<script type="text/html" id="options">--}}
            {{--<div class="layui-btn-group">--}}
            {{--@can('member.member.create')--}}
            {{--<a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>--}}
            {{--@endcan--}}
            {{--@can('member.member.destroy')--}}
            {{--<a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">禁用</a>--}}
            {{--@endcan--}}
            {{--</div>--}}
            {{--</script>--}}

        </div>

    </div>
@endsection
@section('script')

    <script>
        layui.use(['layer', 'table', 'form', 'upload'], function () {
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var upload = layui.upload;
            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                , height: 500
                , url: "{{ route('owner.driver.data') }}" //数据接口
                , page: true //开启分页
                , cols: [[ //表头
                    {checkbox: true, fixed: true},
                    {field: 'id', title: 'ID', sort: true}
                    , {field: 'name', title: '姓名',}
                    , {field: 'phone', title: '手机'}
                    , {field: 'card_no', title: '身份证号',}
                    , {field: 'car_no', title: '车牌号',}
                    , {
                        field: 'status', title: '司机状态', templet: function (e) {
                            if (e.status == 0) {
                                return '删除';
                            } else {
                                return '正常';
                            }

                        }
                    }
                    , {field: 'created_at', title: '注册时间',}


                ]]
            });

            //监听工具条
            table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'del') {
                    $.post('{{route('owner.driver.del')}}', {'id': data.id, 'status': 0}, function (msgjson) {
                        layer.msg(msgjson.msg, {'icon': 5})
                    }, 'json')
                }
            });


            //搜索
            form.on('submit(search-form)', function (data) {
                var fields = data.field;
                dataTable.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: fields // 搜索字段
                });
                return false;
            });
            //立即发送短信，首先询问是否建立标签
            $("#tosend").on('click', function () {
                //判断是否选中
                var ids = []
                var phones = [];
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data;
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id);
                        phones.push(element.phone)
                    })
                }
                if (ids.length <= 0) {
                    layer.msg("请选择手机号", {icon: 5});
                    return false;
                }
                ids = ids.join(',');
                phones = phones.join(',');
                layer.open({
                    title: '确定发送？'
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        //按钮【按钮一】的回调
                        //判断用户是否点击了单选框
                        if ($("#savelab").is(":checked")) {//点击了单选框
//创建标签
                            layer.prompt({
                                formType: 2,
                                btn: ['确定'],
                                value: '',
                                title: '标签名称',
                                text: ['10px', '30px'] //自定义文本域宽高
                            }, function (value, index, elem) {
                                //  alert(value); //得到value
                                //标签入库
                                $.post('{{route('owner.driverlabel.store')}}', {
                                    'ids': ids,
                                    'lable_name': value
                                }, function (datajson) {

                                }, 'json');
                                layer.prompt({
                                    formType: 2,
                                    btn: ['发送'],
                                    value: '',
                                    title: '发送内容',
                                    area: ['500px', '100px'] //自定义文本域宽高
                                }, function (value, index, elem) {
                                    //alert(value); //得到value
                                    //群发短信
                                    $.post('{{route('send.msg.manay')}}', {
                                        'phone': phones,
                                        'msg': value,
                                        'uid':{{$uid}},
                                        'sendsource': 2
                                    }, function (datajson) {
                                        layer.msg(datajson.msg, {icon: 6});

                                    }, 'json');
                                    layer.close(index);
                                });
                                layer.close(index);
                            });

                            layer.close(index);
                        } else {//未点击单选框
                            layer.prompt({
                                formType: 2,
                                btn: ['发送'],
                                value: '',
                                title: '发送内容',
                                area: ['500px', '100px'] //自定义文本域宽高
                            }, function (value, index, elem) {
                                //alert(value); //得到value
                                //群发短信
                                $.post('{{route('send.msg.manay')}}', {
                                    'phone': phones,
                                    'msg': value,
                                    'uid':{{$uid}},
                                    'sendsource': 2
                                }, function (datajson) {
                                    layer.msg(datajson.msg, {icon: 6});

                                }, 'json');
                                layer.close(index);
                            });
                            layer.close(index);
                        }
                    }
                    , btn2: function (index, layero) {
                        //按钮【按钮二】的回调

                        layer.close(index);
                    }
                    , content: '<input type=checkbox id=savelab >存为标签，下次可以直接使用'

                    , cancel: function () {
                        //右上角关闭回调
                        layer.close(index);
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                });


            })

            //创建标签
            $("#toaddLabel").on('click', function () {
                //判断是否选中
                var ids = []
                var phones = [];
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data;
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id);
                        phones.push(element.phone)
                    })
                }
                if (ids.length <= 0) {
                    layer.msg("请选择手机号", {icon: 5});
                    return false;
                }
                ids = ids.join(',');
                phones = phones.join(',');

                layer.confirm('保存为标签，下次可以直接选用?',
                    {
                        icon: 3, title: '提示', btn: ['确定', '取消'],
                        yes: function (index, layeno) {
                            console.log("点击了确定");
                            //创建标签
                            layer.prompt({
                                formType: 2,
                                btn: ['确定'],
                                value: '',
                                title: '标签名称',
                                text: ['10px', '30px'] //自定义文本域宽高
                            }, function (value, index, elem) {
                                //  alert(value); //得到value
                                //标签入库
                                $.post('{{route('owner.driverlabel.store')}}', {
                                    'ids': ids,
                                    'lable_name': value
                                }, function (datajson) {
                                    window.location.href = "{{route('owner.driver.sendmsg')}}";

                                }, 'json');

                                layer.close(index);
                            });

                            layer.close(index);
                        },

                    }
                );

            })

        })


    </script>

@endsection




